package com.gt.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class GetLocationUtil {

    /**
     * 通过腾讯地图将经纬度转成详细地址
     *
     * @param lat 纬度
     * @param lng 经度
     * @param key 开发密钥（Key） 需自行申请
     * @return
     */

    public static JSONObject getLatAndLng(String hsUrl, String lat, String lng, String key) {
        try {
            hsUrl = hsUrl + "?location="+lat+","+lng+"&key="+key+"&get_poi=1";
            String data = HttpClientUtil.doGet(hsUrl);
            JSONObject object = JSONObject.parseObject(data);
            if (object != null) {
                if (null != object.get("status") && object.getInteger("status") == 0) {
                    JSONObject result = object.getJSONObject("result");
                    if (result != null) {
                        JSONObject addressComponent = result.getJSONObject("address_component");
                        if (addressComponent != null) {
                            return addressComponent;
                        }
                    }
                }
            }
            /*log.info("请求地址:{}",hsUrl);
            URL url = new URL(hsUrl);
            HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
            con.setRequestMethod("GET");// 提交模式
            X509TrustManager xtm = new X509TrustManager() {
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    // TODO Auto-generated method stub
                    return null;
                }

                @Override
                public void checkServerTrusted(X509Certificate[] arg0, String arg1)
                        throws CertificateException {
                    // TODO Auto-generated method stub

                }

                @Override
                public void checkClientTrusted(X509Certificate[] arg0, String arg1)
                        throws CertificateException {
                    // TODO Auto-generated method stub

                }
            };

            TrustManager[] tm = {xtm};

            SSLContext ctx = SSLContext.getInstance("TLS");
            ctx.init(null, tm, null);

            con.setSSLSocketFactory(ctx.getSocketFactory());
            con.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            });


            InputStream inStream = con.getInputStream();
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, len);
            }
            byte[] b = outStream.toByteArray();//网页的二进制数据
            outStream.close();
            inStream.close();
            String rtn = new String(b, "utf-8");
            if (StringUtils.isNotBlank(rtn)) {
                JSONObject object = JSONObject.parseObject(rtn);
                if (object != null) {
                    if (null != object.get("status") && object.getInteger("status") == 0) {
                        JSONObject result = object.getJSONObject("result");
                        if (result != null) {
                            JSONObject addressComponent = result.getJSONObject("address_component");
                            if (addressComponent != null) {
                                return addressComponent;
                            }
                        }
                    }
                }
            }*/
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(getLatAndLng("https://apis.map.qq.com/ws/geocoder/v1/","39.984154", "116.307490", "PWSBZ-CJKLF-W4GJO-NA3WU-2ZKGS-IVFTJ"));


//        RestTemplate restTemplate = new RestTemplate();
//        JSONObject json = new JSONObject();
//        String url = "https://apis.map.qq.com/ws/geocoder/v1/";
//        json.put("location","39.984154,116.307490");
//        json.put("key","PWSBZ-CJKLF-W4GJO-NA3WU-2ZKGS-IVFTJ");
//        json.put("get_poi","1");
//        System.out.println(HttpUtil.requestApiGet(json,url,restTemplate));
    }
}
